//
//  YKURLNavigator.m
//  Pods
//
//  Created by wanyakun on 2017/3/13.
//
//

#import "YKURLNavigator.h"

#define CLASS_PREFIX                @"#"
#define NAVTYPE_URL_PUSH            @"yk://push"
#define NAVTYPE_URL_PRESENT         @"yk://present"
#define WEBVIEW_VC_NAME             @"YKWebViewController"
#define YK_VC_NAME                  @"YKViewController"
#define WEBVIEW_URL_PROPERTY        @"webURLString"

@interface YKURLConfigCenter : NSObject {
    NSMutableDictionary *_urlConfigs;
    NSMutableDictionary *_urlAttributes;
}

+ (instancetype)sharedInstance;
- (id)responderForUrl:(NSString *)url;

@end

@implementation YKURLConfigCenter

+ (instancetype)sharedInstance {
    static YKURLConfigCenter *instance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [[YKURLConfigCenter alloc] init];
    });
    return instance;
}

- (instancetype)init {
    if (self = [super init]) {
        _urlConfigs = [[NSMutableDictionary alloc] initWithCapacity:50];
        _urlAttributes = [[NSMutableDictionary alloc] initWithCapacity:50];
        
        NSDictionary *bundleDict = [NSDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"matrix" ofType:@"plist"]];
        NSArray *bundlePlistArray = bundleDict != nil ? [bundleDict objectForKey:@"bundle_list"] : nil;
        for (NSDictionary *bundlePlistItem in bundlePlistArray) {
            NSDictionary *bundleConfig = [NSDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle] pathForResource:bundlePlistItem[@"bundle"] ofType:nil]];
            NSArray *configArray = bundleConfig != nil ? [bundleConfig objectForKey:@"url_handle_list"] : nil;
            for (NSDictionary *config in configArray) {
                id exact_url = [config objectForKey:@"exact_url"];
                id object = [config objectForKey:@"object"];
                id obj = [_urlConfigs objectForKey:exact_url];
                assert(!obj);   //用来检测是否有重复url注册
                [_urlConfigs setObject:object forKey:exact_url];
                
                NSString *classNameKey = object;
                NSArray *array = [config objectForKey:@"attribute"];
                if (array.count > 0) {
                    NSDictionary *attributeDict = [_urlAttributes objectForKey:classNameKey];
                    NSMutableDictionary *attributeMap = nil;
                    if (attributeDict) {
                        attributeMap = [NSMutableDictionary dictionaryWithDictionary:attributeDict];
                    } else {
                        attributeMap = [NSMutableDictionary dictionary];
                    }
                    
                    for (NSDictionary *map in array) {
                        id mapKey = [map objectForKey:@"map_key"];
                        id mapObject = [map objectForKey:@"attr_name"];
                        if (mapKey && mapObject) {
                            [attributeMap setObject:mapObject forKey:mapKey];
                        }
                    }
                    
                    [_urlAttributes setObject:attributeMap forKey:classNameKey];
                }
            }
            
        }
    }
    return self;
}

- (id)responderForUrl:(NSString *)url {
    return [_urlConfigs objectForKey:url];
}

- (NSString *)attributeNameForMapName:(NSString *)mapName withClassName:(NSString *)className {
    className = [CLASS_PREFIX stringByAppendingString:className];
    return [[_urlAttributes objectForKey:className] objectForKey:mapName];
}

@end

@interface YKURLNavigator () {
    NSURL *_originalURL;
}

@end

@implementation YKURLNavigator

- (BOOL)ykOpenUrl:(NSURL *)url withTarget:(id)target params:(id)params {
    BOOL result = YES;
    
    //调用Rewrite引擎
    
    if (!url && url.absoluteString.length == 0) {
        return NO;
    }
    //
    
    result = [self _ykOpenUrl:url withTarget:target params:params];
    
    return result;
}

- (BOOL)_ykOpenUrl:(NSURL *)url withTarget:(id)target params:(id)params {
    if (!url) {
        return NO;
    }
    
    id obj = [self objectForUrl:url withTarget:target params:params];
    if (!obj) {
        return NO;
    }
    
    if ([obj isKindOfClass:[UIViewController class]]) {
        
    }
    
}

- (id)objectForUrl:(NSURL *)url withTarget:(id)target params:(id)params {
    id responder = nil;
    if (url && url.scheme == nil) {
        url = [NSURL URLWithString:[NSString stringWithFormat:@"http:%@", url.absoluteString]];
    }
    
    if ([url.scheme isEqualToString:@"http"] ||
        [url.scheme isEqualToString:@"https"]) {
        
    }
    
    
    return responder;
}


@end
